package com.cqs.leetcode;

import java.util.LinkedList;
import java.util.Random;

public class MyStack {


    private int defaultSize = 1024;
    private int[] stack = new int[defaultSize];
    private int length = 0;

    /**
     * Initialize your data structure here.
     */
    public MyStack() {
    }

    /**
     * Push element x onto stack.
     */
    public void push(int x) {
        if (length == stack.length) {
            //扩容
            int[] newStack = new int[length << 2];
            System.arraycopy(stack, 0, newStack, 0, length);
            stack = newStack;
        }
        stack[length++] = x;

    }

    /**
     * Removes the element on top of the stack and returns that element.
     */
    public int pop() {
        --length;
        return stack[length];
    }

    /**
     * Get the top element.
     */
    public int top() {
        return stack[length - 1];
    }

    /**
     * Returns whether the stack is empty.
     */
    public boolean empty() {
        return length == 0;
    }

    public static void main(String[] args) {
        LinkedList<Integer> list =new LinkedList<>();
        for (int i = 0; i < 10 ; i++) {
            int tmp = new Random().nextInt(100);
            System.out.print(tmp+"\t");
            list.addFirst(tmp);
        }
        System.out.println();
        while (list.size()>0){
            Integer poll = list.poll();
            System.out.print(poll+"\t");
        }
    }
}